home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / nos042_s / pppcmd.c < prev    next >
C/C++ Source or Header  |  1994-09-16  |  14KB  |  625 lines

  1. /*
  2.  *  PPPCMD.C    -- PPP related user commands
  3.  *
  4.  *    This implementation of PPP is declared to be in the public domain.
  5.  *
  6.  *    Jan 91    Bill_Simpson@um.cc.umich.edu
  7.  *        Computer Systems Consulting Services
  8.  *
  9.  *    Acknowledgements and correction history may be found in PPP.C
  10.  */
  11.  
  12. /****************************************************************************
  13. *    $Id: pppcmd.c 1.5 93/07/16 11:48:56 ROOT_DOS Exp $
  14. *    10 Aug 92    1.2        GT    Idle timeout routines.                            *
  15. *    11 Aug 92    1.3        GT    Reopen LCP after close.                            *
  16. *    19 Aug 92    1.4        GT    "exit" option in idle command.                    *
  17. *    14 Jul 93    1.5        GT    Fix warnings.                                    *
  18. ****************************************************************************/
  19.  
  20. #include <stdio.h>
  21. #include "global.h"
  22. #include "mbuf.h"
  23. #include "iface.h"
  24. #include "pktdrvr.h"
  25. #include "ppp.h"
  26. #include "pppfsm.h"
  27. #include "ppplcp.h"
  28. #include "ppppap.h"
  29. #include "pppipcp.h"
  30. #include "cmdparse.h"
  31. #include    "commands.h"
  32.  
  33. static struct iface *ppp_lookup __ARGS((char *ifname));
  34.  
  35. static int doppp_quick        __ARGS((int argc, char *argv[], void *p));
  36. static int doppp_trace        __ARGS((int argc, char *argv[], void *p));
  37. static int doppp_idle __ARGS((int argc, char *argv[], void *p));
  38.  
  39. static int spot __ARGS((int16 work,int16 want,int16 will,int16 mask));
  40. static void genstat        __ARGS((struct ppp_s *ppp_p));
  41. static void lcpstat        __ARGS((struct fsm_s *fsm_p));
  42. static void papstat        __ARGS((struct fsm_s *fsm_p));
  43. static void ipcpstat        __ARGS((struct fsm_s *fsm_p));
  44.  
  45. static int dotry_nak        __ARGS((int argc, char *argv[], void *p));
  46. static int dotry_req        __ARGS((int argc, char *argv[], void *p));
  47. static int dotry_terminate    __ARGS((int argc, char *argv[], void *p));
  48.  
  49. static void idle_timeout __ARGS((void *p));
  50.  
  51.  
  52. /* "ppp" subcommands */
  53. static struct cmds Pppcmds[] = {
  54.     { "ipcp",        doppp_ipcp,    0,    0,    NULLCHAR },
  55.     { "lcp",        doppp_lcp,    0,    0,    NULLCHAR },
  56.     { "pap",        doppp_pap,    0,    0,    NULLCHAR },
  57.     { "quick",    doppp_quick,    0,    0,    NULLCHAR },
  58.     { "trace",    doppp_trace,    0,    0,    NULLCHAR },
  59.     { "idle",        doppp_idle,    0,    0,    NULLCHAR },
  60.     { NULLCHAR },
  61. };
  62.  
  63. /* "ppp <iface> <ncp> try" subcommands */
  64. static struct cmds PppTrycmds[] = {
  65.     { "configure",    dotry_req,    0,    0,    NULLCHAR },
  66.     { "failure",    dotry_nak,    0,    0,    NULLCHAR },
  67.     { "terminate",    dotry_terminate,    0,    0,    NULLCHAR },
  68.     { NULLCHAR },
  69. };
  70.  
  71. static char *PPPStatus[] = {
  72.     "Physical Line Dead",
  73.     "Establishment Phase",
  74.     "Authentication Phase",
  75.     "Network Protocol Phase",
  76.     "Termination Phase"
  77. };
  78.  
  79. static char *NCPStatus[] = {
  80.     "Closed",
  81.     "Listening -- waiting for remote host to attempt open",
  82.     "Starting configuration exchange",
  83.     "Remote host accepted our request; waiting for remote request",
  84.     "We accepted remote request; waiting for reply to our request",
  85.     "Opened",
  86.     "Terminate request sent to remote host"
  87. };
  88.  
  89. int PPPtrace;
  90. struct iface *PPPiface;  /* iface for trace */
  91.  
  92. static int exit_on_idle = 0;            /* nz - exit on idle timeout        */
  93.  
  94.  
  95. /****************************************************************************/
  96.  
  97. static struct iface *
  98. ppp_lookup(ifname)
  99. char *ifname;
  100. {
  101.     register struct iface *ifp;
  102.  
  103.     if ((ifp = if_lookup(ifname)) == NULLIF) {
  104.         tprintf("%s: Interface unknown\n",ifname);
  105.         return(NULLIF);
  106.     }
  107.     if (ifp->type != CL_PPP) {
  108.         tprintf("%s: not a PPP interface\n",ifp->name);
  109.         return(NULLIF);
  110.     }
  111.     return(ifp);
  112. }
  113.  
  114. /****************************************************************************/
  115.  
  116. int
  117. doppp_commands(argc,argv,p)
  118. int argc;
  119. char *argv[];
  120. void *p;
  121. {
  122.     register struct iface *ifp;
  123.  
  124.     if (argc < 2) {
  125.         tprintf( "ppp <iface> required\n" );
  126.         return -1;
  127.     }
  128.     if ((ifp = ppp_lookup(argv[1])) == NULLIF)
  129.         return -1;
  130.  
  131.     if ( argc == 2 ) {
  132.         ppp_show( ifp );
  133.         return 0;
  134.     }
  135.  
  136.     return subcmd(Pppcmds, argc - 1, &argv[1], ifp);
  137. }
  138.  
  139.  
  140. /* Close connection on PPP interface */
  141. int
  142. doppp_close(argc,argv,p)
  143. int argc;
  144. char *argv[];
  145. void *p;
  146. {
  147.     register struct fsm_s *fsm_p = p;
  148.  
  149.     fsm_p->flags &= ~(FSM_ACTIVE | FSM_PASSIVE);
  150.  
  151.     fsm_close( fsm_p );
  152.     return 0;
  153. }
  154.  
  155.  
  156. int
  157. doppp_passive(argc,argv,p)
  158. int argc;
  159. char *argv[];
  160. void *p;
  161. {
  162.     register struct fsm_s *fsm_p = p;
  163.  
  164.     fsm_p->flags &= ~FSM_ACTIVE;
  165.     fsm_p->flags |= FSM_PASSIVE;
  166.  
  167.     fsm_start(fsm_p);
  168.     return 0;
  169. }
  170.  
  171.  
  172. int
  173. doppp_active(argc,argv,p)
  174. int argc;
  175. char *argv[];
  176. void *p;
  177. {
  178.     register struct fsm_s *fsm_p = p;
  179.  
  180.     fsm_p->flags &= ~FSM_PASSIVE;
  181.     fsm_p->flags |= FSM_ACTIVE;
  182.  
  183.     if ( fsm_p->state < fsmLISTEN ) {
  184.         fsm_p->state = fsmLISTEN;
  185.     }
  186.     return 0;
  187. }
  188.  
  189.  
  190. static int
  191. doppp_quick(argc,argv,p)
  192. int argc;
  193. char *argv[];
  194. void *p;
  195. {
  196.     register struct iface *ifp = p;
  197.     register struct ppp_s *ppp_p = ifp->edv;
  198.     struct lcp_s *lcp_p = ppp_p->fsm[Lcp].pdv;
  199.     struct ipcp_s *ipcp_p = ppp_p->fsm[IPcp].pdv;
  200.  
  201.     lcp_p->local.want.accm = 0L;
  202.     lcp_p->local.want.negotiate |= LCP_N_ACCM;
  203.     lcp_p->local.want.magic_number += (long)&lcp_p->local.want.magic_number;
  204.     lcp_p->local.want.negotiate |= LCP_N_MAGIC;
  205.     lcp_p->local.want.negotiate |= LCP_N_ACFC;
  206.     lcp_p->local.want.negotiate |= LCP_N_PFC;
  207.  
  208.     ipcp_p->local.want.compression = PPP_COMPR_PROTOCOL;
  209.     ipcp_p->local.want.slots = 16;
  210.     ipcp_p->local.want.slot_compress = 1;
  211.     ipcp_p->local.want.negotiate |= IPCP_N_COMPRESS;
  212.     doppp_active( 0, NULL, &(ppp_p->fsm[IPcp]) );
  213.  
  214.     return 0;
  215. }
  216.  
  217.  
  218. /****************************************************************************/
  219.  
  220. void
  221. ppp_show(ifp)
  222. struct iface *ifp;
  223. {
  224.     register struct ppp_s *ppp_p = ifp->edv;
  225.  
  226.     genstat(ppp_p);
  227.     if ( ppp_p->fsm[Lcp].pdv != NULL )
  228.         lcpstat(&(ppp_p->fsm[Lcp]));
  229.     if ( ppp_p->fsm[Pap].pdv != NULL )
  230.         papstat(&(ppp_p->fsm[Pap]));
  231.     if ( ppp_p->fsm[IPcp].pdv != NULL )
  232.         ipcpstat(&(ppp_p->fsm[IPcp]));
  233. }
  234.  
  235.  
  236. static void
  237. genstat(ppp_p)
  238. register struct ppp_s *ppp_p;
  239. {
  240.  
  241.     tprintf("%s", PPPStatus[ppp_p->phase]);
  242.  
  243.     if (ppp_p->phase == pppREADY) {
  244.         tprintf("\t(open for %s)",
  245.             tformat(secclock() - ppp_p->upsince));
  246.     }
  247.     tprintf("\n");
  248.  
  249.     tprintf("%10lu In,  %10lu Flags,%6u ME, %6u FE, %6u CSE, %6u other\n",
  250.         ppp_p->InRxOctetCount,
  251.         ppp_p->InOpenFlag,
  252.         ppp_p->InMemory,
  253.         ppp_p->InFrame,
  254.         ppp_p->InChecksum,
  255.         ppp_p->InError);
  256.     tprintf("\t\t%6u Lcp,%6u Pap,%6u IPcp,%6u Unknown\n",
  257.         ppp_p->InNCP[Lcp],
  258.         ppp_p->InNCP[Pap],
  259.         ppp_p->InNCP[IPcp],
  260.         ppp_p->InUnknown);
  261.     tprintf("%10lu Out, %10lu Flags,%6u ME, %6u Fail\n",
  262.         ppp_p->OutTxOctetCount,
  263.         ppp_p->OutOpenFlag,
  264.         ppp_p->OutMemory,
  265.         ppp_p->OutError);
  266.     tprintf("\t\t%6u Lcp,%6u Pap,%6u IPcp\n",
  267.         ppp_p->OutNCP[Lcp],
  268.         ppp_p->OutNCP[Pap],
  269.         ppp_p->OutNCP[IPcp]);
  270. }
  271.  
  272.  
  273. static int spot(
  274.     int16 work,
  275.     int16 want,
  276.     int16 will,
  277.     int16 mask )
  278. {
  279.     char blot = ' ';
  280.     int result = (work & mask);
  281.  
  282.     if ( !(will & mask) ) {
  283.         blot = '*';
  284.     } else if ( (want ^ work) & mask ) {
  285.         blot = (result ? '+' : '-');
  286.     }
  287.     tprintf( "%c", blot );
  288.     return result;
  289. }
  290.  
  291. static void
  292. lcpstat(fsm_p)
  293. struct fsm_s *fsm_p;
  294. {
  295.     struct lcp_s *lcp_p = fsm_p->pdv;
  296.     struct lcp_value_s *localp = &(lcp_p->local.work);
  297.     int16  localwork = lcp_p->local.work.negotiate;
  298.     int16  localwant = lcp_p->local.want.negotiate;
  299.     int16  localwill = lcp_p->local.will_negotiate;
  300.     struct lcp_value_s *remotep = &(lcp_p->remote.work);
  301.     int16  remotework = lcp_p->remote.work.negotiate;
  302.     int16  remotewant = lcp_p->remote.want.negotiate;
  303.     int16  remotewill = lcp_p->remote.will_negotiate;
  304.  
  305.     tprintf("LCP %s\n",
  306.         NCPStatus[fsm_p->state]);
  307.  
  308.     tprintf("\t\t MRU\t ACCM\t\t AP\t PFC  ACFC Magic\n");
  309.  
  310.     tprintf("\tLocal:\t");
  311.  
  312.     spot( localwork, localwant, localwill, LCP_N_MRU );
  313.     tprintf( "%4d\t", localp->mru );
  314.  
  315.     spot( localwork, localwant, localwill, LCP_N_ACCM );
  316.     tprintf( "0x%08lx\t", localp->accm );
  317.  
  318.     if ( spot( localwork, localwant, localwill, LCP_N_AUTHENT ) ) {
  319.         switch ( localp->authentication ) {
  320.         case PPP_PAP_PROTOCOL:
  321.             tprintf( "Pap\t" );
  322.             break;
  323.         default:
  324.             tprintf( "0x%04x\t", localp->authentication);
  325.             break;
  326.         };
  327.     } else {
  328.         tprintf( "None\t" );
  329.     }
  330.  
  331.     tprintf( spot( localwork, localwant, localwill, LCP_N_PFC )
  332.          ? "Yes " : "No  " );
  333.     tprintf( spot( localwork, localwant, localwill, LCP_N_ACFC )
  334.          ? "Yes " : "No  " );
  335.  
  336.     spot( localwork, localwant, localwill, LCP_N_MAGIC );
  337.     if ( localp->magic_number != 0L ) {
  338.         tprintf( "0x%08lx\n", localp->magic_number );
  339.     } else {
  340.         tprintf( "unused\n" );
  341.     }
  342.  
  343.     tprintf("\tRemote:\t");
  344.  
  345.     spot( remotework, remotewant, remotewill, LCP_N_MRU );
  346.     tprintf( "%4d\t", remotep->mru );
  347.  
  348.     spot( remotework, remotewant, remotewill, LCP_N_ACCM );
  349.     tprintf( "0x%08lx\t", remotep->accm );
  350.  
  351.     if ( spot( remotework, remotewant, remotewill, LCP_N_AUTHENT ) ) {
  352.         switch ( remotep->authentication ) {
  353.         case PPP_PAP_PROTOCOL:
  354.             tprintf( "Pap\t" );
  355.             break;
  356.         default:
  357.             tprintf( "0x%04x\t", remotep->authentication);
  358.             break;
  359.         };
  360.     } else {
  361.         tprintf( "None\t" );
  362.     }
  363.  
  364.     tprintf( spot( remotework, remotewant, remotewill, LCP_N_PFC )
  365.          ? "Yes " : "No  " );
  366.     tprintf( spot( remotework, remotewant, remotewill, LCP_N_ACFC )
  367.          ? "Yes " : "No  " );
  368.  
  369.     spot( remotework, remotewant, remotewill, LCP_N_MAGIC );
  370.     if ( remotep->magic_number != 0L ) {
  371.         tprintf( "0x%08lx\n", remotep->magic_number );
  372.     } else {
  373.         tprintf( "unused\n" );
  374.     }
  375. }
  376.  
  377.  
  378. static void
  379. papstat(fsm_p)
  380. struct fsm_s *fsm_p;
  381. {
  382.     struct pap_s *pap_p = fsm_p->pdv;
  383.  
  384.     tprintf("PAP %s\n",
  385.         NCPStatus[fsm_p->state]);
  386.  
  387.     tprintf( "\tMessage: '%s'\n", (pap_p->message == NULL) ?
  388.         "none" : pap_p->message );
  389. }
  390.  
  391.  
  392. static void
  393. ipcpstat(fsm_p)
  394. struct fsm_s *fsm_p;
  395. {
  396.     struct ipcp_s *ipcp_p = fsm_p->pdv;
  397.     struct ipcp_value_s *localp = &(ipcp_p->local.work);
  398.     int16  localwork = ipcp_p->local.work.negotiate;
  399.     struct ipcp_value_s *remotep = &(ipcp_p->remote.work);
  400.     int16  remotework = ipcp_p->remote.work.negotiate;
  401.  
  402.     tprintf("IPCP %s\n",
  403.         NCPStatus[fsm_p->state]);
  404.     tprintf("\tlocal IP address: %s",
  405.         inet_ntoa(localp->address));
  406.     tprintf("  remote IP address: %s\n",
  407.         inet_ntoa(localp->other));
  408.  
  409.     if (localwork & IPCP_N_COMPRESS) {
  410.         tprintf("    In\tTCP header compression enabled:"
  411.             " slots = %d, flag = 0x%02x\n",
  412.             localp->slots,
  413.             localp->slot_compress);
  414.         slhc_i_status(ipcp_p->slhcp);
  415.     }
  416.  
  417.     if (remotework & IPCP_N_COMPRESS) {
  418.         tprintf("    Out\tTCP header compression enabled:"
  419.             " slots = %d, flag = 0x%02x\n",
  420.             remotep->slots,
  421.             remotep->slot_compress);
  422.         slhc_o_status(ipcp_p->slhcp);
  423.     }
  424. }
  425.  
  426.  
  427. /****************************************************************************/
  428. /* Set timeout interval when waiting for response from remote peer */
  429. int
  430. doppp_timeout(argc,argv,p)
  431. int argc;
  432. char *argv[];
  433. void *p;
  434. {
  435.     struct fsm_s *fsm_p = p;
  436.     struct timer *t = &(fsm_p->timer);
  437.  
  438.     if (argc < 2) {
  439.         tprintf("%d\n",dur_timer(t)/1000L);
  440.     } else {
  441.         int x = (int)strtol( argv[1], NULLCHARP, 0 );
  442.  
  443.         if (x <= 0) {
  444.             tprintf("Timeout value %s (%d) must be > 0\n",
  445.                 argv[1], x);
  446.             return -1;
  447.         } else {
  448.             set_timer(t, x * 1000L);
  449.         }
  450.     }
  451.     return 0;
  452. }
  453.  
  454.  
  455. int
  456. doppp_try(argc,argv,p)
  457. int argc;
  458. char *argv[];
  459. void *p;
  460. {
  461.     return subcmd(PppTrycmds, argc, argv, p);
  462. }
  463.  
  464.  
  465. static int
  466. dotry_nak(argc,argv,p)
  467. int argc;
  468. char *argv[];
  469. void *p;
  470. {
  471.     struct fsm_s *fsm_p = p;
  472.  
  473.     if (argc < 2) {
  474.         tprintf("%d\n",fsm_p->try_nak);
  475.     } else {
  476.         int x = (int)strtol( argv[1], NULLCHARP, 0 );
  477.  
  478.         if (x <= 0) {
  479.             tprintf("Value %s (%d) must be > 0\n",
  480.                 argv[1], x);
  481.             return -1;
  482.         } else {
  483.             fsm_p->try_nak = x;
  484.         }
  485.     }
  486.     return 0;
  487. }
  488.  
  489.  
  490. static int
  491. dotry_req(argc,argv,p)
  492. int argc;
  493. char *argv[];
  494. void *p;
  495. {
  496.     struct fsm_s *fsm_p = p;
  497.  
  498.     if (argc < 2) {
  499.         tprintf("%d\n",fsm_p->try_req);
  500.     } else {
  501.         int x = (int)strtol( argv[1], NULLCHARP, 0 );
  502.  
  503.         if (x <= 0) {
  504.             tprintf("Value %s (%d) must be > 0\n",
  505.                 argv[1], x);
  506.             return -1;
  507.         } else {
  508.             fsm_p->try_req = x;
  509.         }
  510.     }
  511.     return 0;
  512. }
  513.  
  514.  
  515. static int
  516. dotry_terminate(argc,argv,p)
  517. int argc;
  518. char *argv[];
  519. void *p;
  520. {
  521.     struct fsm_s *fsm_p = p;
  522.  
  523.     if (argc < 2) {
  524.         tprintf("%d\n",fsm_p->try_terminate);
  525.     } else {
  526.         int x = (int)strtol( argv[1], NULLCHARP, 0 );
  527.  
  528.         if (x <= 0) {
  529.             tprintf("Value %s (%d) must be > 0\n",
  530.                 argv[1], x);
  531.             return -1;
  532.         } else {
  533.             fsm_p->try_terminate = x;
  534.         }
  535.     }
  536.     return 0;
  537. }
  538.  
  539.  
  540. static int
  541. doppp_trace(argc,argv,p)
  542. int argc;
  543. char *argv[];
  544. void *p;
  545. {
  546.     register struct iface *ifp = p;
  547.     register struct ppp_s *ppp_p = ifp->edv;
  548.     int tracing = ppp_p->trace;
  549.     int result = setint(&tracing,"PPP tracing",argc,argv);
  550.  
  551.     ppp_p->trace = tracing;
  552.     return result;
  553. }
  554.  
  555.  
  556. /****************************************************************************
  557. *    doppp_idle                                                                *
  558. *    Set/display the PPP idle timer.                                            *
  559. ****************************************************************************/
  560.  
  561. static int doppp_idle (argc, argv, p)
  562. int argc;
  563. char *argv[];
  564. void *p;
  565.     {
  566.     struct iface *ifp = p;                /* -> interface                        */
  567.     struct ppp_s *ppp_p = ifp->edv;        /* -> PPP control structure            */
  568.  
  569.     if (argc < 2)
  570.         {
  571.         tprintf ("PPP idle timeout: %ld\n", ppp_p->idle_durn / 1000L);
  572.         tprintf ("Exit on idle timeout: %s\n",
  573.                  (exit_on_idle == 0) ? "off" : "on");
  574.         }
  575.     else
  576.         {
  577.         int32 x = strtol (argv[1], NULLCHARP, 0);
  578.  
  579.         if (x < 0)
  580.             {
  581.             tprintf ("Timeout value %s (%ld) must be >= 0\n",
  582.                      argv[1], x);
  583.             return (-1);
  584.             }
  585.         else
  586.             {
  587.             if (ppp_p->idle_durn != 0L)
  588.                 stop_timer (&(ppp_p->idle_timer));
  589.                 
  590.             ppp_p->idle_durn = x * 1000L;
  591.             ppp_p->idle_timer.func = idle_timeout;
  592.             ppp_p->idle_timer.arg = &(ppp_p->fsm[Lcp]);
  593.             set_timer (&(ppp_p->idle_timer), ppp_p->idle_durn);
  594.             if (argc == 3)
  595.                 {
  596.                 if (strcmp (argv[2], "exit") == 0)
  597.                     exit_on_idle = 1;
  598.                 else if (strcmp (argv[2], "noexit") == 0)
  599.                     exit_on_idle = 0;
  600.  
  601.                 }
  602.                     
  603.             }
  604.  
  605.         }
  606.         
  607.     return (0);
  608.     }    /* static int doppp_idle (argc, argv, p) */
  609.  
  610.  
  611. /****************************************************************************
  612. *    idle_timeout                                                            *
  613. *    Performs an "lcp close" when the idle timer times out.                    *
  614. ****************************************************************************/
  615.  
  616. static void idle_timeout (p)
  617. void *p;
  618.     {
  619.     (void) doppp_close (0, NULL, p);
  620.     if (exit_on_idle != 0)
  621.         doexit (0, NULL, NULL);
  622.         
  623.     (void) dolcp_open (0, NULL, p);
  624.     }    /* static void idle_timeout (p) */
  625.